package br.ufpe.cin.wormbox.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import javax.swing.JProgressBar;
import javax.swing.UIManager;

import br.ufpe.cin.wormbox.core.FachadaUsuario;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import java.util.regex.*;

public class guiCadastro extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8509361981867389695L;
	private JPanel contentPane;
	private JTextField textNome;
	private JTextField textLogin;
	private JPasswordField textSenha;
	private JPasswordField textSenha2;
	private cadastroKeyListener CKL;
	private CadastroActionListener CAL;
	public final JFrame frame;
	private JProgressBar progressBar;
	private JLabel lblFraca;
	private FachadaUsuario fachada;
	private JButton btnOk;
	private JButton button;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					guiCadastro frame = new guiCadastro(null, null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public guiCadastro(JFrame f, FachadaUsuario fachada) {
		this.frame = f;
		this.fachada = fachada;
		
		this.CKL = new cadastroKeyListener(f);
		this.CAL = new CadastroActionListener(this);
		
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				frame.setEnabled(true);
			}
		});
		setBounds(100, 100, 555, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNome = new JLabel("Nome");
		lblNome.setBounds(147, 102, 40, 14);
		contentPane.add(lblNome);
		
		textNome = new JTextField();
		textNome.setColumns(10);
		textNome.setBounds(187, 97, 170, 20);
		contentPane.add(textNome);
		
		JLabel lblLabel = new JLabel("Login");
		lblLabel.setBounds(147, 132, 40, 14);
		contentPane.add(lblLabel);
		
		textLogin = new JTextField();
		textLogin.setBounds(187, 127, 170, 20);
		contentPane.add(textLogin);
		textLogin.setColumns(10);
		
		JLabel lblSenha = new JLabel("Senha");
		lblSenha.setBounds(147, 162, 40, 14);
		contentPane.add(lblSenha);
		
		btnOk = new JButton("Voltar");
		btnOk.setBounds(224, 252, 99, 23);
		contentPane.add(btnOk);
		btnOk.addActionListener(CAL);
		
		textSenha = new JPasswordField();
		textSenha.setBounds(187, 157, 170, 20);
		contentPane.add(textSenha);
		textSenha.addKeyListener(CKL);
		
		progressBar = new JProgressBar();
		progressBar.setBounds(367, 162, 99, 14);
		progressBar.setValue(30);
		contentPane.add(progressBar);
		
		button = new JButton("Criar conta");
		button.setBounds(187, 218, 170, 23);
		contentPane.add(button);
		button.addActionListener(CAL);
		
		JLabel lblLoginIndisponvel = new JLabel("Login Indispon\u00EDvel");
		lblLoginIndisponvel.setBounds(367, 132, 128, 14);
		contentPane.add(lblLoginIndisponvel);
		
		lblFraca = new JLabel("Fraca");
		lblFraca.setBounds(477, 162, 40, 14);
		contentPane.add(lblFraca);
		
		JLabel label = new JLabel("");
		label.setBounds(0, 0, 46, 14);
		contentPane.add(label);
		
		JLabel lblCadastro = new JLabel("");
		lblCadastro.setIcon(new ImageIcon("icons\\LogoCadastro.png"));
		lblCadastro.setFont(new Font("Gisha", Font.BOLD, 25));
		lblCadastro.setBounds(149, 31, 231, 75);
		contentPane.add(lblCadastro);
		
		JLabel lblSenha2 = new JLabel("Digite a senha novamente");
		lblSenha2.setBounds(52, 192, 135, 14);
		contentPane.add(lblSenha2);
		
		textSenha2 = new JPasswordField();
		textSenha2.setBounds(187, 187, 170, 20);
		contentPane.add(textSenha2);
		
		JLabel label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon("icons\\balao2.png"));
		label_1.setBounds(42, 286, 287, 147);
		contentPane.add(label_1);
		
		JLabel labelMinhoca2 = new JLabel("");
		labelMinhoca2.setIcon(new ImageIcon("icons\\Minhoca Grande.png"));
		labelMinhoca2.setBounds(261, 315, 256, 269);
		contentPane.add(labelMinhoca2);
		
		JLabel labelFundo2 = new JLabel("");
		labelFundo2.setIcon(new ImageIcon("icons\\fundo.jpg"));
		labelFundo2.setBounds(-271, -262, 820, 769);
		contentPane.add(labelFundo2);
				
	}
	
	class CadastroActionListener implements ActionListener {
		private guiCadastro f;
		
		public CadastroActionListener(guiCadastro f) {
			this.f = f;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			Object src = e.getSource();
			
			if(src == button) {
				try {
					String s1 = new String(textSenha.getPassword()), s2 = new String(textSenha2.getPassword());
					if(s1.equals(s2)) {
						if(fachada.cadastrar(textNome.getText(), textLogin.getText(), s1)) {
							JOptionPane.showMessageDialog(rootPane, "Cadastro realizado com sucesso");
						} else {
							JOptionPane.showMessageDialog(rootPane, "Usuario ja registrado");
						}
					} else {
						JOptionPane.showMessageDialog(rootPane, "As senhas devem ser iguais!");
					}
					
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(rootPane, "Erro inesperado no cadastro");
				}
			} else if (src == btnOk) {
				f.frame.setEnabled(true);
				f.setVisible(false);
			}
			
			
		}
	}
	
	class cadastroKeyListener implements KeyListener {

		private JFrame f;
		
		public cadastroKeyListener(JFrame f) {
			this.f = f;
		}

		@Override
		public void keyPressed(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyTyped(KeyEvent e) {
			Object src = e.getSource();
			
			if(src == textSenha) {
				Pattern p1 = Pattern.compile("[a-z]+"),
						p2 = Pattern.compile("[a-z0-9]+"),
						p3 = Pattern.compile("[^ ]+");
				Matcher m1, m2, m3;
				
				char c = e.getKeyChar();
				
				if(c == ' ') {
					e.consume();
				} else {
					String txt;
					if (e.getKeyChar() == KeyEvent.VK_BACK_SPACE) {
						txt = new String(textSenha.getPassword());
						txt = txt.substring(0,  txt.length() - 1);
					} else {
						txt = new String(textSenha.getPassword()) + c;
					}
					m1 = p1.matcher(txt);
					m2 = p2.matcher(txt);
					m3 = p3.matcher(txt);
					int len = txt.length();
					
					
					if (len > 3) {
						if (m1.matches()) {
							if(len > 7) {
								progressBar.setValue(65);
								lblFraca.setText("Media");
							} else {
								progressBar.setValue(30);
								lblFraca.setText("Fraca");
							}
						} else if (m2.matches()) {
							if(len > 5) {
								progressBar.setValue(65);
								lblFraca.setText("Media");
							} else {
								progressBar.setValue(30);
								lblFraca.setText("Fraca");
							}
						} else if (m3.matches()) {
							if(len > 5) {
								progressBar.setValue(100);
								lblFraca.setText("Forte");
							} else {
								progressBar.setValue(65);
								lblFraca.setText("Media");
							}
						}
					} else {
						progressBar.setValue(30);
						lblFraca.setText("Fraca");
					}
				}
			}
		}
		
	}
}
